package com.apps.ftapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.LessonNewAdapter;
import com.apps.ftapp.model.LessonModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class BanxingActivity extends BaseActivity {

    private ListView banxing;
    private LessonNewAdapter adapter;
    private final Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banxing);
        setTitle("班型");
        init();
    }

    private void init() {
        banxing = findViewById(R.id.banxing_list);
        List<LessonModel.DataBean> list = new ArrayList<>();
        adapter = new LessonNewAdapter(BanxingActivity.this, R.layout.fragment_lesson_listview_item, list);
        banxing.setAdapter(adapter);
        banxing.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LessonModel.DataBean data = (LessonModel.DataBean) adapterView.getAdapter().getItem(i);
                HashMap<String, Object> map = new HashMap();
                map.put("id", data.getId());
                map.put("title", data.getTitle());
                map.put("type", data.getType());
                ActivitySkipUtil.skipAnotherActivity(BanxingActivity.this, LessonDetailActivity.class, map);
            }
        });
        loaddata();
    }

    private void loaddata() {
        RequestParams params = new RequestParams();
        params.put("userId", Constant.token);
        Async.post("course/banxing", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    LessonModel result = gson.fromJson(new String(responseBody), LessonModel.class);
                    if (null != result.getData()) {
                        adapter.addAll(result.getData());
                    }
                }
            }


            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
                    error) {

            }
        });

    }


}
